import { ref } from 'vue'
import { findTopCategory } from '@/api/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

function useCategory () {
  const categroyAllList = ref([])
  const route = useRoute() // route this.$route  route.params.id
  async function getCategroyAllList (id = route.params.id) {
    const res = await findTopCategory(id)
    categroyAllList.value = res.result.children
  }
  getCategroyAllList()
  // 执行路由更新函数
  onBeforeRouteUpdate((to) => {
    // 获取id
    getCategroyAllList(to.params.id)
  })
  return {
    categroyAllList
  }
}

export default useCategory
